<template>
  <div class="divBox relative">
    <el-card class="box-card" style="min-height: 80vh">
      <div slot="header" class="clearfix" style="margin-bottom: -20px ;">
        <div class="container">
          <el-form :inline="true" :model="queryForm" @submit.native.prevent>
            <el-form-item>
              <el-select v-model="queryForm.appid" placeholder="请选择app" class="width100" filterable clearable>
                <el-option
                  v-for="item in applist"
                  :key="item.appid"
                  :value="item.appid"
                  :label="item.appname">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item v-if="this.selOptions.JavaInfo.level===0">
              <el-select v-model="queryForm.orgid" placeholder="请选择机构" class="width100" filterable clearable >
                <el-option
                  v-for="item in orglist"
                  :key="item.id"
                  :value="item.id"
                  :label="item.orgname">
                </el-option>
              </el-select>
            </el-form-item>

            <el-form-item>
              <el-input v-model="queryForm.id" placeholder="请输入用户ID" clearable
                        @keyup.enter.native="search()"
              ></el-input>
            </el-form-item>

            <el-form-item>
              <el-input v-model="queryForm.username" placeholder="请输入用户名" clearable
                        @keyup.enter.native="search()"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-input v-model="queryForm.phone" placeholder="请输入手机号" clearable
                        @keyup.enter.native="search()"
              ></el-input>
            </el-form-item>

            <el-form-item>
              <el-select v-model="queryForm.partnerstatus" placeholder="请选择身份" class="width100" clearable
                         filterable
              >
                <el-option
                  v-for="item in selOptions.partnerstatus"
                  :key="item.value"
                  :value="item.value"
                  :label="item.label"
                >
                </el-option>
              </el-select>
            </el-form-item>

            <el-form-item>
              <el-input v-model="queryForm.deviceid" placeholder="请输入设备ID" clearable
                        @keyup.enter.native="search()"
              ></el-input>
            </el-form-item>

            <el-form-item>
              <el-input v-model="queryForm.totalcoin_begin" placeholder="请输入累计金币" clearable
                        @keyup.enter.native="search()"></el-input>
            </el-form-item>
            <el-form-item>
              <el-input v-model="queryForm.coin_begin" placeholder="请输入当前金币" clearable
                        @keyup.enter.native="search()"></el-input>
            </el-form-item>


            <el-form-item>
              <el-button @click="search()">查询</el-button>
              <!--              <el-button v-if="checkPermi(['loginuser:index:save'])" type="primary" @click="addOrUpdateHandle()">新增</el-button>-->
              <el-button v-if="checkPermi(['loginuser:index:delete'])"  type="danger" @click="deleteHandle1()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
              <el-button v-if="checkPermi(['loginuser:index:editRate1'])"  type="danger" @click="changeRatebatch(1,'请输入兑换比例')" :disabled="dataListSelections.length <= 0">兑换比例设置</el-button>
              <el-button v-if="checkPermi(['loginuser:index:editRate2'])"  type="danger" @click="changeRatebatch(2,'请输入系统抽成比例')" :disabled="dataListSelections.length <= 0">系统抽成设置</el-button>
              <el-button v-if="checkPermi(['loginuser:index:editRate3'])"  type="danger" @click="changeRatebatch(3,'请输入上级抽成比例')" :disabled="dataListSelections.length <= 0">上级抽成设置</el-button>
              <el-button v-if="checkPermi(['loginuser:index:editRate3'])"  type="danger" @click="changeRatebatch(4,'请输入机构ID')" :disabled="dataListSelections.length <= 0">机构设置</el-button>
              <el-button v-if="checkPermi(['loginuser:index:editRate3'])"  type="danger" @click="changeRatebatch(5,'请输入身份信息')" :disabled="dataListSelections.length <= 0">身份设置</el-button>
              <el-button v-if="checkPermi(['loginuser:index:editRate3'])"  type="danger" @click="changeRatebatch(6,'请输入参数信息')" :disabled="dataListSelections.length <= 0">参数设置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>



<!--      <div  style="border-bottom:1px solid #e6ebf5;padding: 0px 10px 20px">-->
<!--        <div align="middle" >-->
<!--          <a-row :gutter="24" class="ivu-mt">-->
<!--            <a-col :span="8">-->
<!--              <a-statistic title="累计金币" :value="res.sumtotalcoin" />-->
<!--            </a-col>-->
<!--            <a-col :span="8">-->
<!--              <a-statistic title="当前金币" :value="res.sumcoin" ></a-statistic>-->
<!--            </a-col>-->
<!--            <a-col :span="8" >-->
<!--              <a-statistic title="钱包余额" :precision="2" :value="res.sumamount/100.0" />-->
<!--            </a-col>-->
<!--          </a-row>-->
<!--        </div>-->
<!--      </div>-->


      <el-table :data="dataList" v-loading="dataListLoading" v-fit-columns @selection-change="selectionChangeHandle"
                style="width: 100%;" size="mini" :highlight-current-row="true"
                :header-cell-style=" {fontWeight:'bold'}"
      >
        <el-table-column type="selection" header-align="center" align="center" width="50"/>
        <el-table-column prop="id" label="用户信息"  width="150px">
          <template slot-scope="scope">
            {{ scope.row.id }} - {{ scope.row.username }}
          </template>
        </el-table-column>
        <el-table-column prop="phone" label="手机号" width="150px">
        </el-table-column>
        <el-table-column prop="note" label="备注">
        </el-table-column>
        <el-table-column prop="tag" label="真实姓名">
        </el-table-column>
        <el-table-column prop="deviceid" label="设备ID">
        </el-table-column>

        <el-table-column prop="totalcoin" label="累计金币">
        </el-table-column>
        <el-table-column prop="coin" label="当前金币">
        </el-table-column>

        <el-table-column prop="lockcoin" label="锁定金币">
        </el-table-column>

        <el-table-column prop="amount" label="钱包">
          <template slot-scope="scope">
            {{ scope.row.amount / 100.0 }}
          </template>
        </el-table-column>
        <el-table-column  v-if="this.selOptions.JavaInfo.level===0" prop="conversionrate" label="兑换比例">
          <template slot-scope="scope">
            {{ scope.row.conversionrate }}%
          </template>
        </el-table-column>
        <el-table-column prop="sysextractionrate" label="系统抽成" v-if="this.selOptions.JavaInfo.level===0">
          <template slot-scope="scope">
            {{ scope.row.sysextractionrate }}%
          </template>
        </el-table-column>
<!--        <el-table-column prop="extractionrate" label="上级抽成">-->
<!--          <template slot-scope="scope">-->
<!--            {{ scope.row.extractionrate }}%-->
<!--          </template>-->
<!--        </el-table-column>-->
        <el-table-column   v-if="this.selOptions.JavaInfo.level===0" prop="usufruct" label="参数设置">
        </el-table-column>

        <el-table-column prop="partnerstatus" label="身份">
          <template slot-scope="scope">
            <el-tag size="mini" v-if="scope.row.partnerstatus==0">普通用户</el-tag>
            <el-tag type="success" size="mini" v-if="scope.row.partnerstatus==1">合作伙伴</el-tag>
            <el-tag type="danger" size="mini" v-if="scope.row.partnerstatus==2">员工号</el-tag>
            <el-tag type="warning" size="mini" v-if="scope.row.partnerstatus==3">特殊账户</el-tag>
          </template>
        </el-table-column>
        <el-table-column  v-if="this.selOptions.JavaInfo.level===0" prop="vipstatus" label="vip等级">
        </el-table-column>

        <el-table-column prop="cdkcount" label="cdk数量" width="150px">
        </el-table-column>


        <!--        <el-table-column prop="username" label="上级代理" >-->
        <!--          <template slot-scope="scope">-->
        <!--            {{ scope.row.puserid }} - {{ scope.row.pusername }}-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <el-table-column prop="ruserid" label="上级代理">
        </el-table-column>
        <el-table-column prop="appname" label="app" width="120">
        </el-table-column>
        <el-table-column prop="orgname" label="机构" width="120" v-if="this.selOptions.JavaInfo.level===0">
        </el-table-column>

        <!--        <el-table-column prop="invitecode" label="邀请码" width="150px">-->
        <!--        </el-table-column>-->

        <el-table-column prop="lockstatus" label="状态"  v-if="this.selOptions.JavaInfo.level===0">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.lockstatus==0"
              :active-value="true"
              :inactive-value="false"
              active-text="正常"
              inactive-text="冻结"
              @change="change(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column prop="createtime" label="创建时间" width="150px">
        </el-table-column>
        <el-table-column prop="logintime" label="最后活跃时间" width="150px">
        </el-table-column>

        <el-table-column v-if="checkPermi(['loginuser:index:update'])&&checkPermi(['loginuser:index:delete'])"
                         fixed="right" header-align="center" align="center" width="150" label="操作"
        >
          <template slot-scope="scope">
            <el-button type="text" v-if="checkPermi(['loginuser:index:update'])" size="small"
                       @click="addOrUpdateHandle(scope.row.id)"
            >修改
            </el-button>
<!--            <el-button type="text" v-if="checkPermi(['orginfo:index:delete'])" size="small"-->
<!--                       @click="deleteHandle1(scope.row.id)"-->
<!--            >删除-->
<!--            </el-button>-->
            <el-button type="text" v-if="checkPermi(['loginuser:index:share'])" size="small"
                       @click="changeRate(scope.row)"
            >分成比例
            </el-button>
            <el-button type="text" v-if="checkPermi(['loginuser:index:delete'])" size="small"
                       @click="deleteHandle(scope.row.id)"
            >重置密码
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="tableForm.pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="tableForm.pageSize"
        :total="tableForm.total"
        layout="total, sizes, prev, pager, next, jumper"
      >
      </el-pagination>
    </el-card>
    <!-- 表单弹窗, 新增数据和修改数据 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './loginuser-add-and-update.vue'
import api from '@/api/api'
import { checkPermi } from '@/utils/permission'
import utils from '@/api/utils'
import Cookies from 'js-cookie'

export default {
  components: {
    AddOrUpdate
  },
  data() {
    return {
      selOptions: {},
      dataList: [],
      applist:[],
      orglist:[],
      queryForm: {
        id: '',
        appid: '',
        orgid: '',
        username: '',
        deviceid: '',
        pusername: '',
        password: '',
        avatar: '',
        bgimg: '',
        sex: '',
        birthday: '',
        phone: '',
        email: '',
        province: '',
        city: '',
        region: '',
        level: '',
        note: '',
        tag: '',
        lockstatus: '',
        type: '',
        status: '',
        authstatus: '',
        adminid: '',
        createtime: '',
        updatetime: '',
        owenerinvitecode: '',
        invitecode: '',
        coin: '',
        amount: '',
        point: '',
        openid: '',
        wxappid: '',
        vipstatus: '',
        vipendtime: '',
        onlinestatus: '',
        createtime_begin: '',
        createtime_end: '',
        totalcoin_begin: '',
        coin_begin: '',
        todaycoin_begin: '',
        timeVal: []
      },
      res:{},
      tableForm: {
        pageIndex: 1,
        pageSize: 10,
        total: 0
      },
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  mounted() {
    this.selOptions = this.$constants.selOptions
    utils.getAppList().then(res => {this.applist=res.datalist});
    utils.getOrgList().then(res => {this.orglist=res.datalist});
    this.search()
  },
  methods: {
    checkPermi,
    // 每页数
    sizeChangeHandle(val) {
      this.tableForm.pageSize = val
      this.tableForm.pageIndex = 1
      this.search()
    },
    // 当前页
    currentChangeHandle(val) {
      this.tableForm.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    change(row) {
      let postData = { id: row.id }
      postData.lockstatus = row.lockstatus == 1 ? 0 : 1
      api.post('rest/vabc/loginuser/update', postData).then(res => {
        this.$message.success('操作成功')
        row.lockstatus = postData.lockstatus
        // this.getDataList()
      })
    },
    // 删除
    deleteHandle1(id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`您确定对[id=${ids.join(',')}]进行[${id ? '删除' : '删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let postData = { ids: ids.join(',') }
        api.post('rest/vabc/loginuser/delete', postData).then(res => {
          this.$message.success('删除成功')
          this.getDataList()
        })
      })
    },
    deleteHandle(id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`您确定对[id=${ids.join(',')}]进行[${id ? '密码重置' : '密码重置'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let postData = { ids: ids.join(',') }
        api.post('rest/vabc/loginuser/resertPwdByBoss', postData).then(res => {
          this.$message.success('密码重置为123456')
          this.getDataList()
        })
      })
    },
    changeRatebatch(type,message) {
      this.$prompt(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputType: 'text',
        inputPattern: /^.+$/,
        inputErrorMessage: '请输入内容'
      }).then(({ value }) => {
        var ids = this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`您确定对本批数据进行重新设置吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let postData = { ids: ids.join(',') }
          postData.rate = type==6?'':value
          postData.usufruct = value
          postData.type = type
          api.post('rest/vabc/loginuser/resertRate', postData).then(res => {
            this.$message.success('重置成功')
            this.getDataList()
          })
        })
      }).catch(() => {
      })
    },

    search() {
      this.tableForm.pageIndex = 1
      this.getDataList()
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      this.dataListLoading = true
      let postData = {
        'order': 'desc',
        'column': 'id'
      }
      Object.assign(postData, this.queryForm, this.tableForm)
      //postData.name=api.getLikeValue( postData.name);
      postData.username = api.getLikeValue(postData.username)
      postData.password = api.getLikeValue(postData.password)
      postData.avatar = api.getLikeValue(postData.avatar)
      postData.bgimg = api.getLikeValue(postData.bgimg)
      postData.sex = api.getLikeValue(postData.sex)
      postData.birthday = api.getLikeValue(postData.birthday)
      postData.phone = api.getLikeValue(postData.phone)
      postData.email = api.getLikeValue(postData.email)
      postData.province = api.getLikeValue(postData.province)
      postData.city = api.getLikeValue(postData.city)
      postData.region = api.getLikeValue(postData.region)
      postData.level = api.getLikeValue(postData.level)
      postData.note = api.getLikeValue(postData.note)
      postData.tag = api.getLikeValue(postData.tag)
      postData.lockstatus = api.getLikeValue(postData.lockstatus)
      postData.type = api.getLikeValue(postData.type)
      postData.status = api.getLikeValue(postData.status)
      postData.authstatus = api.getLikeValue(postData.authstatus)
      postData.createtime = api.getLikeValue(postData.createtime)
      postData.updatetime = api.getLikeValue(postData.updatetime)
      postData.owenerinvitecode = api.getLikeValue(postData.owenerinvitecode)
      postData.invitecode = api.getLikeValue(postData.invitecode)
      postData.coin = api.getLikeValue(postData.coin)
      postData.amount = api.getLikeValue(postData.amount)
      postData.point = api.getLikeValue(postData.point)
      postData.openid = api.getLikeValue(postData.openid)
      postData.wxappid = api.getLikeValue(postData.wxappid)
      postData.vipendtime = api.getLikeValue(postData.vipendtime)
      postData.onlinestatus = api.getLikeValue(postData.onlinestatus)
      api.post('rest/vabc/loginuser/list', postData).then(res => {
        this.res=res
        this.dataList = res.datalist
        this.tableForm.total = res.total
      }).finally(() => {
        this.dataListLoading = false
      })
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    changeRate(row) {
      this.$prompt('请输入分成比例', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputType: 'number',
        inputPattern: /^.+$/,
        inputErrorMessage: '请输入内容'
      }).then(({ value }) => {
        let postData = { id: row.id }
        postData.extractionrate = value
        api.post('rest/vabc/loginuser/updateByID', postData).then(res => {
          this.$message.success('操作成功')
          row.extractionrate = value
        })
      }).catch(() => {
        // this.$message({
        //   type: 'info',
        //   message: '取消输入'
        // });
      })
    },
    onchangeTime(e) {
      this.queryForm.createtime_begin = e[0]
      this.queryForm.createtime_end = e[1]
    }
  }
}
</script>
